//
//  XHMainScrollView.m
//  HarmonizePub
//
//  Created by Haven on 2016/11/3.
//  Copyright © 2016年 Zy. All rights reserved.
//

#import "XHMainScrollView.h"
#import "Config.h"
#import "HealthyBKCell.h"
#import "XHHealthyScrollViewCell.h"
#import "XHHospitalDTCell.h"
#import "MJRefresh.h"
#import "MJExtension.h"
#import "HealthyInfoList.h"
#import "InformationWebViewViewController.h"


@interface XHMainScrollView()<UITableViewDelegate, UITableViewDataSource, UIScrollViewDelegate>

@property (nonatomic, strong) NSArray *healthyBKLists;
@property (nonatomic, strong) NSArray *healthyDTLists;
@property (nonatomic, strong) NSArray *healthyJYLists;
@property (nonatomic, strong) NSMutableArray *tempBKLists;
@property (nonatomic, strong) NSMutableArray *tempDTLists;
@property (nonatomic, strong) NSMutableArray *tempJYLists;

@property (nonatomic, assign) int curPageBK;
@property (nonatomic, assign) int curPageDT;
@property (nonatomic, assign) int curPageJY;
@property (nonatomic, assign) int pageSize;


@end

@implementation XHMainScrollView
static NSString *HealthyBKCellId = @"HealthyBKCell";
static NSString *HospitalDTCellId = @"XHHospitalDTCell";

- (NSMutableArray *)tempBKLists
{
    if (_tempBKLists == nil) {
        _tempBKLists = [NSMutableArray array];
    }
    return _tempBKLists;
}
- (NSMutableArray *)tempDTLists
{
    if (_tempDTLists == nil) {
        _tempDTLists = [NSMutableArray array];
    }
    return _tempDTLists;
}
- (NSMutableArray *)tempJYLists
{
    if (_tempJYLists == nil) {
        _tempJYLists = [NSMutableArray array];
    }
    return _tempJYLists;
}

- (void)awakeFromNib
{
    [super awakeFromNib];
    self.healthyType = HealthyTypeBK;
    self.curPageBK = 1;
    self.curPageDT = 1;
    self.curPageJY = 1;
    self.pageSize = 8;
    self.backgroundColor = [UIColor whiteColor];
    // 设置scrollView属性
    [self setContentSize:CGSizeMake(XHScreenWidth*3, 0)];
    [self setFrame: CGRectMake(0, 44, XHScreenWidth, XHScreenHeight - 152)];
    
    // 创建内部视图
    [self createContaintView];
    
    
    // 注册cell
    UINib *healthyBKCellNib = [UINib nibWithNibName:[HealthyBKCell registerName] bundle:nil];
    UINib *hospitalDTCellNib = [UINib nibWithNibName:[XHHospitalDTCell registerName] bundle:nil];
    [self.HealthyBKTableView registerNib: healthyBKCellNib forCellReuseIdentifier:HealthyBKCellId];
    [self.HealthyJYTableView registerNib: healthyBKCellNib forCellReuseIdentifier:HealthyBKCellId];
    [self.HealthyBKTableView registerNib: hospitalDTCellNib forCellReuseIdentifier:HospitalDTCellId];
    [self.HospitalDTTableView registerNib: hospitalDTCellNib forCellReuseIdentifier:HospitalDTCellId];
    [self.HealthyJYTableView registerNib: hospitalDTCellNib forCellReuseIdentifier:HospitalDTCellId];
    
    // 初始化刷新控件
    [self upDownLoadRefresh];
    
    
    // 加载数据
    [self loadHealthyData];

    

}

// 加载数据
- (void)loadHealthyData
{
    NSMutableDictionary *paramsMu=[[NSMutableDictionary alloc]initWithObjectsAndKeys:@(self.healthyType), @"type", @(self.pageSize),@"pageSize", nil];
    if (self.healthyType == HealthyTypeBK) {
        [paramsMu setValue:@(self.curPageBK) forKey: @"curPage"];
    }else if (self.healthyType == HealthyTypeDT) {
        [paramsMu setValue:@(self.curPageDT) forKey: @"curPage"];
    }else if (self.healthyType == HealthyTypeJY){
        [paramsMu setValue:@(self.curPageJY) forKey: @"curPage"];
    }
    NSDictionary *params = (NSDictionary *)paramsMu;
    NSDictionary *cookie = @{CookieVaule : [UserDefaultsTool stringForKey:XHToken]};
    // 发送请求
    [HTTPSTool POSTRequestWithPath:XHGetHelathInfoList cookie:cookie params:params success:^(NSDictionary * _Nonnull JSON) {
//        LRLog(@"%@", JSON);
        NSArray *tempArr = JSON[@"data"][@"list"];
        // 加载tableView数据
        [self loadDataWithArray: tempArr];
        
        }failure:^(NSError * _Nonnull error) {
    }];
}

- (void)loadDataWithArray: (NSArray *)tempArr{
    if (self.healthyType == HealthyTypeBK) {
        if (tempArr.count) {
            [self.tempBKLists addObjectsFromArray: tempArr];
        }
        // 字典数组转模型数组
        self.healthyBKLists = [HealthyInfoList  mj_objectArrayWithKeyValuesArray: self.tempBKLists];
        
        // 刷新表格
        [self.HealthyBKTableView reloadData];
        
        // 拿到当前的上拉刷新控件，结束刷新状态
        [self.HealthyBKTableView.mj_footer endRefreshing];
        
        // 返回数据小于等于当前也数的时候, 当前页加1
        if (tempArr.count <= self.pageSize) {
            self.curPageBK++;
        }
        // 返回数据等于0时, 显示已加载完毕
        if (tempArr.count == 0) {
            [self.HealthyBKTableView.mj_footer endRefreshingWithNoMoreData];
        }
        // 总数等于0时, 隐藏刷新控件
        if (self.healthyBKLists.count == 0) {
            [self.HealthyBKTableView.mj_footer setHidden:YES];
            
        }
    }else if (self.healthyType == HealthyTypeDT){
        if (tempArr.count) {
            [self.tempDTLists addObjectsFromArray: tempArr];
        }
        // 字典数组转模型数组
        self.healthyDTLists = [HealthyInfoList  mj_objectArrayWithKeyValuesArray: self.tempDTLists];
        
        // 刷新表格
        [self.HospitalDTTableView reloadData];
        
        // 拿到当前的上拉刷新控件，结束刷新状态
        [self.HospitalDTTableView.mj_footer endRefreshing];
        
        // 返回数据小于等于当前也数的时候, 当前页加1
        if (tempArr.count <= self.pageSize) {
            self.curPageDT++;
        }
        // 返回数据等于0时, 显示已加载完毕
        if (tempArr.count == 0) {
            [self.HospitalDTTableView.mj_footer endRefreshingWithNoMoreData];
        }
        // 总数等于0时, 隐藏刷新控件
        if (self.healthyDTLists.count == 0) {
            [self.HospitalDTTableView.mj_footer setHidden:YES];
        }
    }else{
        if (tempArr.count) {
            [self.tempJYLists addObjectsFromArray: tempArr];
        }
        // 字典数组转模型数组
        self.healthyJYLists = [HealthyInfoList  mj_objectArrayWithKeyValuesArray: self.tempJYLists];
        
        // 刷新表格
        [self.HealthyJYTableView reloadData];
        
        // 拿到当前的上拉刷新控件，结束刷新状态
        [self.HealthyJYTableView.mj_footer endRefreshing];
        
        // 返回数据小于等于当前也数的时候, 当前页加1
        if (tempArr.count <= self.pageSize) {
            self.curPageJY++;
        }
        // 返回数据等于0时, 显示已加载完毕
        if (tempArr.count == 0) {
            [self.HealthyJYTableView.mj_footer endRefreshingWithNoMoreData];
        }
        // 总数等于0时, 隐藏刷新控件
        if (self.healthyJYLists.count == 0) {
            [self.HealthyJYTableView.mj_footer setHidden:YES];
        }
    }
}

- (void)setHealthyType:(HealthyType)healthyType
{
    _healthyType = healthyType;
    if (healthyType != HealthyTypeBK) {
        if (self.curPageDT == 1 || self.curPageJY == 1) {
            
            [self loadHealthyData];
        }
    }
}

// 创建内部视图
- (void)createContaintView
{
    
    for (int i = 0; i < 3; i ++) {
        UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(XHScreenWidth * i, 0, XHScreenWidth, self.bounds.size.height) style:UITableViewStylePlain];
        tableView.delegate = self;
        tableView.dataSource = self;
        tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
        [self addSubview:tableView];
        if (i == 0) {
            self.HealthyBKTableView = tableView;
            
        }else if ( i == 1){
            self.HospitalDTTableView = tableView;
            
        }else{
            self.HealthyJYTableView = tableView;
        }
    }
}

#pragma mark UITableView + 上拉刷新
// 上拉刷新
- (void)upDownLoadRefresh
{
    // 设置回调（一旦进入刷新状态，就调用target的action，也就是调用self的loadMoreData方法）
    self.HealthyBKTableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
    self.HospitalDTTableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
    self.HealthyJYTableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
}

// 上拉加载数据
- (void)loadMoreData
{
    // 获取健康百科列表
    [self loadHealthyData];
    
}

#pragma mark -- UITableViewDelegate, UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (self.healthyType == HealthyTypeDT) {
        
        return self.healthyDTLists.count;
        
    }else if (self.healthyType == HealthyTypeJY){
        
        return self.healthyJYLists.count;
        
    }else{
        
        return self.healthyBKLists.count;
    }
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
     HealthyBKCell *healthyBKCell = [tableView dequeueReusableCellWithIdentifier:HealthyBKCellId];
    
     XHHospitalDTCell *hospitalDTCell = [tableView dequeueReusableCellWithIdentifier:HospitalDTCellId];
    
    if (tableView == self.HospitalDTTableView) {  // 医院动态
        HealthyInfoList *healthyInfoListDT = self.healthyDTLists[indexPath.row];
        
        return [self CreateTableViewCell: healthyInfoListDT healthyBKCell: nil hospitalDTCell: hospitalDTCell];
        
    }else if (tableView == self.HealthyBKTableView){  // 健康百科
        HealthyInfoList *healthyInfoListBK = self.healthyBKLists[indexPath.row];
        
        return [self CreateTableViewCell: healthyInfoListBK healthyBKCell: healthyBKCell hospitalDTCell: hospitalDTCell];

    }else{  // 健康教育
        HealthyInfoList *healthyInfoListJY = self.healthyJYLists[indexPath.row];
        
        return [self CreateTableViewCell: healthyInfoListJY healthyBKCell: healthyBKCell hospitalDTCell: hospitalDTCell];
    }
}

- (UITableViewCell *)CreateTableViewCell: (HealthyInfoList *)healthyInfoList healthyBKCell: (HealthyBKCell *)healthyBKCell hospitalDTCell: (XHHospitalDTCell *)hospitalDTCell
{
    if (healthyInfoList.image.length == 0 || healthyBKCell == nil) {
        
        hospitalDTCell.healthyInfoList = healthyInfoList;
        
        return hospitalDTCell;
        
    }else{
        
        healthyBKCell.healthyInfoList = healthyInfoList;
        
        return  healthyBKCell;
    }

}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.healthyType == HealthyTypeDT) {
        // 健康动态
        HealthyInfoList *healthyInfoListDT = self.healthyDTLists[indexPath.row];
        if ([self.delegate respondsToSelector:@selector(tableViewdidSelectRowWithModel:)]) {
            [self.delegate tableViewdidSelectRowWithModel:healthyInfoListDT];
        }
    }else if (self.healthyType == HealthyTypeJY){
        // 健康教育
        HealthyInfoList *healthyInfoListJY = self.healthyJYLists[indexPath.row];
        if ([self.delegate respondsToSelector:@selector(tableViewdidSelectRowWithModel:)]) {
            [self.delegate tableViewdidSelectRowWithModel:healthyInfoListJY];
        }
    }else{
        // 健康百科
        HealthyInfoList *healthyInfoListBK = self.healthyBKLists[indexPath.row];
        if ([self.delegate respondsToSelector:@selector(tableViewdidSelectRowWithModel:)]) {
            [self.delegate tableViewdidSelectRowWithModel:healthyInfoListBK];
        }
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 80;
}

@end
